//
//  PasswordTableViewCell.m
//  Village
//
//  Created by longrise on 2017/9/8.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "PasswordTableViewCell.h"

#import "Masonry.h"

@interface PasswordTableViewCell ()<UITextFieldDelegate>
@end
@implementation PasswordTableViewCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self addAllViews];
        
    }
    return self;
}

- (void)addAllViews
{
    
    _leftlable = [[UILabel alloc] init];
    _leftlable.text = @"我的文";
    _leftlable.backgroundColor = [UIColor whiteColor];
    _leftlable.font = [UIFont systemFontOfSize:16*kScreenScaleX];
    [_leftlable setTextAlignment : NSTextAlignmentLeft] ;
    [self.contentView addSubview:_leftlable];

    [_leftlable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(21*kScreenScaleX);//使左边等于self.view的左边，间距为0
        //        make.top.equalTo(self.mas_top).offset(10);//使顶部与self.ScrollWebView的底部间距为0
        make.width.mas_equalTo(85*kScreenScaleX);
        make.height.mas_equalTo(30*kScreenScaleY);
        make.centerY.equalTo(self);
    }];
    
    _textfield = [[UITextField alloc] init];
    _textfield.placeholder = @"password";
    _textfield.delegate = self;
    _textfield.font = [UIFont systemFontOfSize:16*kScreenScaleX];
    [self.contentView addSubview:_textfield];
    [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_leftlable.mas_right).offset(10*kScreenScaleX);//使左边等于self.view的左边，间距为0
        //        make.top.equalTo(self.mas_top).offset(10);//使顶部与self.ScrollWebView的底部间距为0
        make.width.mas_equalTo(250*kScreenScaleX);
        make.height.mas_equalTo(30*kScreenScaleY);
        make.centerY.equalTo(self);
    }];
    _seebutton = [[UIButton alloc] init];
    [_seebutton setImage:[UIImage imageNamed:@"unopen"] forState:UIControlStateNormal];
    [_seebutton addTarget:self action:@selector(pwdTextSwitch:)forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_seebutton];
    
    [_seebutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-25*kScreenScaleX);//使左边等于self.view的左边，间距为0
        //        make.top.equalTo(self.mas_top).offset(10);//使顶部与self.ScrollWebView的底部间距为0
        make.width.mas_equalTo(13);
        make.height.mas_equalTo(14);
        make.centerY.equalTo(self);
    }];
    _gainbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    _gainbutton.backgroundColor = [UIColor whiteColor];
    [_gainbutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _gainbutton.titleLabel.font = [UIFont systemFontOfSize:16*kScreenScaleX];
    [_gainbutton addTarget:self action:@selector(gain) forControlEvents:UIControlEventTouchUpInside];
    [_gainbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _gainbutton.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
    [_gainbutton.layer setBorderColor:[UIColor redColor].CGColor];
    _gainbutton.layer.borderWidth = 0.5f*kScreenScaleX;//设置边框颜色
    [self.contentView addSubview:_gainbutton];
    
    [_gainbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-10*kScreenScaleX);//使左边等于self.view的左边，间距为0
        //        make.top.equalTo(self.mas_top).offset(10);//使顶部与self.ScrollWebView的底部间距为0
        make.width.mas_equalTo(100*kScreenScaleX);
        make.height.mas_equalTo(35*kScreenScaleY);
        make.centerY.equalTo(self);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:@"Second" object:nil];

    
}


- (void)notification{
    //点击button 关闭可交互 背景颜色变为灰色
    _gainbutton.userInteractionEnabled = NO;
    _gainbutton.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
    [_gainbutton.layer setBorderColor:[UIColor redColor].CGColor];
    _gainbutton.layer.borderWidth = 0.5f*kScreenScaleX;//设置边框颜色
    
    [self createTimer];

}

- (void)gain{
      //同时创建计时器 开始倒计时
      if (self.gainClickBlock) {
        self.gainClickBlock();
    
    }
}

#pragma mark - 定时器 (GCD)
- (void)createTimer {
    
    //设置倒计时时间
    //通过检验发现，方法调用后，timeout会先自动-1，所以如果从15秒开始倒计时timeout应该写16
    //__block 如果修饰指针时，指针相当于弱引用，指针对指向的对象不产生引用计数的影响
    __block int timeout = 60;
    
    //获取全局队列
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    //创建一个定时器，并将定时器的任务交给全局队列执行(并行，不会造成主线程阻塞)
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, global);
    
    // 设置触发的间隔时间
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    //1.0 * NSEC_PER_SEC  代表设置定时器触发的时间间隔为1s
    //0 * NSEC_PER_SEC    代表时间允许的误差是 0s
    
    //block内部 如果对当前对象的强引用属性修改 应该使用__weak typeof(self)weakSelf 修饰  避免循环调用
    __weak typeof(self)weakSelf = self;
    //设置定时器的触发事件
    dispatch_source_set_event_handler(timer, ^{
        
        //倒计时  刷新button上的title ，当倒计时时间为0时，结束倒计时
        
        //1. 每调用一次 时间-1s
        timeout --;
        
        //2.对timeout进行判断时间是停止倒计时，还是修改button的title
        if (timeout <= 0) {
            
            //停止倒计时，button打开交互，背景颜色还原，title还原
            
            //关闭定时器
            dispatch_source_cancel(timer);
            
            //MRC下需要释放，这里不需要
            //            dispatch_realse(timer);
            
            //button上的相关设置
            //注意: button是属于UI，在iOS中多线程处理时，UI控件的操作必须是交给主线程(主队列)
            //在主线程中对button进行修改操作
            dispatch_async(dispatch_get_main_queue(), ^{
                
                weakSelf.gainbutton.userInteractionEnabled = YES;
                _gainbutton.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
                [_gainbutton.layer setBorderColor:[UIColor redColor].CGColor];
                _gainbutton.layer.borderWidth = 0.5f*kScreenScaleX;//设置边框颜色
                [ weakSelf.gainbutton setTitle:@"获取验证码" forState:UIControlStateNormal];
            });
        }else {
            
            //处于正在倒计时，在主线程中刷新button上的title，时间-1秒
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString * title = [NSString stringWithFormat:@"%ds",timeout];
                
                [ weakSelf.gainbutton setTitle:title forState:UIControlStateNormal];
            });
        }
    });
    
    dispatch_resume(timer);
}


- (IBAction)pwdTextSwitch:(UIButton *)sender {
    
    // 前提:在xib中设置按钮的默认与选中状态的背景图
    // 切换按钮的状态
    sender.selected = !sender.selected;
    
    if (!sender.selected) { // 按下去了就是明文
         [_seebutton setImage:[UIImage imageNamed:@"unopen"] forState:UIControlStateNormal];
        NSString *tempPwdStr = _textfield.text;
        _textfield.text = @""; // 这句代码可以防止切换的时候光标偏移
        _textfield.secureTextEntry = YES;
        _textfield.text = tempPwdStr;
        
    } else { // 暗文
         [_seebutton setImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal];
    NSString *tempPwdStr = _textfield.text;
    _textfield.text = @"";
    _textfield.secureTextEntry = NO;
    _textfield.text = tempPwdStr;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
